#import "LayoutManagersViewController.h"
#import "LayoutManagers.h"

@implementation LayoutManagersViewController

- (void)loadView
{
    UIView *container = [[UIView alloc] initWithFrame:
                         [[UIScreen mainScreen] applicationFrame]];
    self.view = container;
    [container release];

    mainLayout = [[VLayoutView alloc] initWithFrame:CGRectZero spacing:10];
    mainLayout.backgroundColor = [UIColor darkGrayColor];
    [container addSubview:mainLayout];
    
    [self sizingAndMarginsExample];
    [self alignmentExample];
    
    mainLayout.autoresizesSubviews = NO;
    [mainLayout sizeToFit];
    mainLayout.autoresizesSubviews = YES;    
}

- (void)sizingAndMarginsExample
{
    HLayoutView *hlay = [[HLayoutView alloc] initWithFrame:CGRectZero spacing:0
                         leftMargin:3 rightMargin:3 topMargin:3 bottomMargin:3];
    hlay.backgroundColor = [UIColor grayColor];
    [mainLayout addSubview:hlay];
    [hlay release];
    
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor redColor];
    label.text = @"Sizing and Margins Example";
    [label sizeToFit];
    [hlay addSubview:label];
    [label release];
    
    // We are setting the size for the first time, so subviews should keep their
    // current size.
    hlay.autoresizesSubviews = NO;
    [hlay sizeToFit];
    hlay.autoresizesSubviews = YES;
}

- (void)alignmentExample
{
    // Right Top
    
    HLayoutView *rtHLayout
        = [[HLayoutView alloc] initWithFrame:CGRectZero spacing:2
           leftMargin:3 rightMargin:3 topMargin:3 bottomMargin:3
           hAlignment:UIControlContentHorizontalAlignmentRight
           vAlignment:UIControlContentVerticalAlignmentTop];
    rtHLayout.backgroundColor = [UIColor grayColor];
    [mainLayout addSubview:rtHLayout];
    [rtHLayout release];
    
    UILabel *rtLeftLabel = [[UILabel alloc] init];
    rtLeftLabel.backgroundColor = [UIColor redColor];
    rtLeftLabel.text = @"RT Left";
    rtLeftLabel.font = [rtLeftLabel.font fontWithSize:24];
    [rtLeftLabel sizeToFit];
    [rtHLayout addSubview:rtLeftLabel];
    [rtLeftLabel release];
    UILabel *rtRightLabel = [[UILabel alloc] init];
    rtRightLabel.backgroundColor = [UIColor blueColor];
    rtRightLabel.text = @"RT Right";
    [rtRightLabel sizeToFit];
    [rtHLayout addSubview:rtRightLabel];
    [rtRightLabel release];
    
    rtHLayout.autoresizesSubviews = NO;
    [rtHLayout sizeToFit];
    rtHLayout.frame = CGRectMake(0, 0, 250, rtHLayout.frame.size.height);
    rtHLayout.autoresizesSubviews = YES;    

    // Center Center
    
    HLayoutView *ccHLayout
        = [[HLayoutView alloc] initWithFrame:CGRectZero spacing:2
           leftMargin:3 rightMargin:3 topMargin:3 bottomMargin:3
           hAlignment:UIControlContentHorizontalAlignmentCenter
           vAlignment:UIControlContentVerticalAlignmentCenter];
    ccHLayout.backgroundColor = [UIColor grayColor];
    [mainLayout addSubview:ccHLayout];
    [ccHLayout release];
    
    UILabel *ccLeftLabel = [[UILabel alloc] init];
    ccLeftLabel.backgroundColor = [UIColor redColor];
    ccLeftLabel.text = @"CC Left";
    ccLeftLabel.font = [ccLeftLabel.font fontWithSize:24];
    [ccLeftLabel sizeToFit];
    [ccHLayout addSubview:ccLeftLabel];
    [ccLeftLabel release];
    UILabel *ccRightLabel = [[UILabel alloc] init];
    ccRightLabel.backgroundColor = [UIColor blueColor];
    ccRightLabel.text = @"CC Right";
    [ccRightLabel sizeToFit];
    [ccHLayout addSubview:ccRightLabel];
    [ccRightLabel release];
    
    ccHLayout.autoresizesSubviews = NO;
    [ccHLayout sizeToFit];
    ccHLayout.frame = CGRectMake(0, 0, 250, ccHLayout.frame.size.height);
    ccHLayout.autoresizesSubviews = YES;    

    // Left Bottom
    
    HLayoutView *lbHLayout
        = [[HLayoutView alloc] initWithFrame:CGRectZero spacing:2
           leftMargin:3 rightMargin:3 topMargin:3 bottomMargin:3
           hAlignment:UIControlContentHorizontalAlignmentLeft
           vAlignment:UIControlContentVerticalAlignmentBottom];
    lbHLayout.backgroundColor = [UIColor grayColor];
    [mainLayout addSubview:lbHLayout];
    [lbHLayout release];
    
    UILabel *lbLeftLabel = [[UILabel alloc] init];
    lbLeftLabel.backgroundColor = [UIColor redColor];
    lbLeftLabel.text = @"LB Left";
    lbLeftLabel.font = [lbLeftLabel.font fontWithSize:24];
    [lbLeftLabel sizeToFit];
    [lbHLayout addSubview:lbLeftLabel];
    [lbLeftLabel release];
    UILabel *lbRightLabel = [[UILabel alloc] init];
    lbRightLabel.backgroundColor = [UIColor blueColor];
    lbRightLabel.text = @"LB Right";
    [lbRightLabel sizeToFit];
    [lbHLayout addSubview:lbRightLabel];
    [lbRightLabel release];
    
    lbHLayout.autoresizesSubviews = NO;
    [lbHLayout sizeToFit];
    lbHLayout.frame = CGRectMake(0, 0, 250, lbHLayout.frame.size.height);
    lbHLayout.autoresizesSubviews = YES;    
}

- (void)dealloc
{
    [mainLayout release];
    [super dealloc];
}

@end
